package com.google.coffeeshop.gui.desktop.product;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

import com.google.coffeeshop.business.LoadingBusiness;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductGroupFrame extends JFrame{
	private ProductGroupTable productGroupTable;
	public ProductGroupFrame() {
		setTitle("Product Group");
		setSize(600, 335);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(60dlu;default)"),
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(129dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(68dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("397px:grow"),},
			new RowSpec[] {
				RowSpec.decode("14px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JLabel lblProductGroupCode = new JLabel("Code");
		getContentPane().add(lblProductGroupCode, "2, 3, right, top");
		
		txtCode = new JTextField();
		getContentPane().add(txtCode, "5, 3, fill, default");
		txtCode.setColumns(10);
		
		JLabel lblProductGroupName = new JLabel("Name");
		getContentPane().add(lblProductGroupName, "2, 5, right, default");
		
		txtName = new JTextField();
		getContentPane().add(txtName, "5, 5, fill, default");
		txtName.setColumns(10);
		
		JLabel lblProductGroupDes = new JLabel("Description");
		getContentPane().add(lblProductGroupDes, "2, 7, right, default");
		
		txtDes = new JTextField();
		getContentPane().add(txtDes, "5, 7, fill, default");
		txtDes.setColumns(10);
		
		JButton btnSave = new JButton("save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddOrEditGroup();
			}

			
		});
		getContentPane().add(btnSave, "5, 9");
		
		
		
		productGroupTable = new ProductGroupTable();
		getContentPane().add(new JScrollPane(productGroupTable), "5, 11, 4, 1, fill, default");
		
		
		putAllGroupToTable(productGroupTable);
	}
	private void putAllGroupToTable(ProductGroupTable groupTable) {
		productGroupTable.addNewRows(LoadingBusiness.getAllProductGroup());
	}
	
	private void resetForm(){
		txtCode.setText("");
		txtName.setText("");
		txtDes.setText("");
	}
	private void AddOrEditGroup() {
				ProductGroupDTO productGroupDTO = new ProductGroupDTO();
				productGroupDTO.setCode(txtCode.getText());
				productGroupDTO.setName(txtName.getText());
				productGroupDTO.setDescription(txtDes.getText());
				ManagerBusiness.saveProductGroup(productGroupDTO);
				resetForm();
				productGroupTable.resetTableModel();
				putAllGroupToTable(productGroupTable);
			}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtCode;
	private JTextField txtName;
	private JTextField txtDes;

}
